 /*
 * comm_wifi.c
 *
 *  Created on: Oct 15, 2021
 *      Author: Administrator
 */
#include "wifi_comm.h"
#include "r_cg_sau.h"
#include "machine_stat.h"

extern uint8_t dis_smartlink;
extern uint8_t wifi_ctrl_block_time;
extern Time wifi_unlink_time;
uint8_t wifi_cmd =0 ;
uint8_t smartlink=0;
uint8_t get_unlink_feedback=0;
uint16_t wifi_unlink_wait_period=15; //状态机执行周期个数
uint8_t agree_send_wifi_start_msg=1; //用于判断是否同意发送配网开始消息
//uint8_t dis_signal=0;
//用于检测WiFi发来数据是否正确，1是发来数据有误，0是无误
uint8_t dis_comm=0;
WIFI_Stat wifi_stat;
//uint8_t iz=6;

uint8_t send_buf[60]={0};
extern uint8_t check_buf[30];
//uint8_t check_buf[60]={0};
uint8_t wifi_get_buff[60]={0};
//uint8_t wifi_buf_buff[60]={0};
WIFI_send wifi_heater_info;
uint8_t wifi_commtimes = 0;
extern uint8_t key_flag;
extern uint8_t key_num;
extern uint8_t disakey_cycle;
extern volatile uint8_t wifi_UART_send;
extern uint8_t cmd_times;
extern uint8_t get_f2_buff[60];
extern uint8_t dis_f2_send;
extern uint8_t get_f0_buff[60];
extern uint8_t super_is_ok;
extern uint8_t success_wifi;

extern Time wifi_signal;

uint8_t dis_runmode=0;
extern uint8_t last_check_num;
extern uint8_t success_wifi;

//CONNECT_WIFI wifi_connect;
//用于回复WiFi信号强度信息
uint8_t dis_signal_stat=0;
//用于记录超时重发次数，超过三就放弃。
uint8_t dis_count = 0;
//uint8_t dishhhhhh=0;
//uint8_t ji1=0;
//uint8_t ji2=0;
//uint8_t ji0=0;
RUN_mode runing_mode[3]={{42,35,60},{35,35,42},{42,35,45}};

//aaron,解网串口数组
void wifi_unlink_message(void)
{
	uint8_t length;
	
	length = 2;
	send_buf[0] = 0x02;
	send_buf[1] = 0x09;
	send_buf[2] = length;
	send_buf[3] = 0x53;
	send_buf[4] = 0x10; //data1
	send_buf[5] = 0x0a;
	send_buf[6] = 0x03;
	send_buf[7] = checksum(send_buf+1,send_buf[2]+4);
}
//aaron,开始smartlink配网
void wifi_start_massage(void)
{
	uint8_t length;

	length = 3;
	send_buf[0] = 0x02;
	send_buf[1] = 0x09;
	send_buf[2] = length;
	send_buf[3] = TEXT_SET;
	send_buf[4] = CMD_SOFTWEAR_VER;  //data1
	send_buf[5] = 0x08;  //data2
	send_buf[6] = 0x31; 
	send_buf[7] = 0x03; //ETX
	send_buf[8] = checksum(send_buf+1,send_buf[2]+4); //	

}
void wifi_infor_massage(void)
{
	uint8_t length;

	length = 20;
	send_buf[0] = 0x02;
	send_buf[1] = 0x09;
	send_buf[2] = length;
	send_buf[3] = TEXT_INF;
	send_buf[4] = 0xf1;  //data1 temp
	send_buf[5] = 0x00;  //data2
	send_buf[6] = heater_info.SetHeatTemp;  //data3  err code
	send_buf[7] = 0x00;  //data4
	send_buf[8] = heater_info.err_code_h;  //data4
	send_buf[9] = heater_info.err_code_l;  //data5	err type
	send_buf[10] = heater_info.err_type;  //data6	fire
	send_buf[11] = heater_info.fire_stat;  //data7	power
	send_buf[12] = heater_info.power_stat;  //data8	eco
	send_buf[13] = heater_info.eco_stat;  //data9	
	send_buf[14] = heater_info.eco_mode;  //data10	temp max
	send_buf[15] = runing_mode[dis_runmode].max_temp;  //data11
	send_buf[16] = 0x00;  //data12	temp min
	send_buf[17] = runing_mode[dis_runmode].min_temp;  //data13
	send_buf[18] = 0x00;  //data14	cycle switch
	send_buf[19] = heater_info.akey_cycle;  //data15	cycle mode
//	send_buf[20] = heater_info.cycle_book;  //data16	cycle book
	send_buf[20] = heater_info.cycle_mode;  //data17	super
	send_buf[21] = heater_info.super_mode;  //data18	cold
	send_buf[22] = heater_info.cold_stat;  //data19	run mode
	send_buf[23] = heater_info.running_mode;
	send_buf[24] = 0x03; //ETX
	send_buf[25] = checksum(send_buf+1,send_buf[2]+4); //	

}

void wifi_f1_massage(void)
{
	uint8_t length;

	length = 20;
	send_buf[0] = 0x02;
	send_buf[1] = 0x09;
	send_buf[2] = length;
	send_buf[3] = TEXT_RES;
	send_buf[4] = 0xf1;  //data1 temp
	send_buf[5] = 0x00;  //data2
	send_buf[6] = heater_info.SetHeatTemp;  //data3  err code
	send_buf[7] = 0x00;  //data4
	send_buf[8] = heater_info.err_code_h;  //data4
	send_buf[9] = heater_info.err_code_l;  //data5	err type
	send_buf[10] = heater_info.err_type;  //data6	fire
	send_buf[11] = heater_info.fire_stat;  //data7	power
	send_buf[12] = heater_info.power_stat;  //data8	eco
	send_buf[13] = heater_info.eco_stat;  //data9	
	send_buf[14] = heater_info.eco_mode;  //data10	temp max
	send_buf[15] = runing_mode[dis_runmode].max_temp;  //data11
	send_buf[16] = 0x00;  //data12	temp min
	send_buf[17] = runing_mode[dis_runmode].min_temp;  //data13
	send_buf[18] = 0x00;  //data14	cycle switch
	send_buf[19] = heater_info.akey_cycle;  //data15	cycle mode
//	send_buf[20] = heater_info.cycle_book;  //data16	cycle book
	send_buf[20] = heater_info.cycle_mode;  //data17	super
	send_buf[21] = heater_info.super_mode;  //data18	cold
	send_buf[22] = heater_info.cold_stat;  //data19	run mode
	send_buf[23] = heater_info.running_mode;
	send_buf[24] = 0x03; //ETX
	send_buf[25] = checksum(send_buf+1,send_buf[2]+4); //	

}
void wifi_ack_massage(void)
{

	uint8_t length;
	
	length = 0;
	send_buf[0] = 0x02;
	send_buf[1] = 0x09;
	send_buf[2] = length;
	send_buf[3] = 0x41;
	send_buf[4] = 0x03;  
	send_buf[5] = checksum(send_buf+1,send_buf[2]+4); //
}
void wifi_NAK_massage(void)
{

	uint8_t length;
	
	length = 0;
	send_buf[0] = 0x02;
	send_buf[1] = 0x09;
	send_buf[2] = length;
	send_buf[3] = 0x4E;
	send_buf[4] = 0x03;  
	send_buf[5] = checksum(send_buf+1,send_buf[2]+4); //
}
void wifi_reset_data(void)
{
	uint8_t length;
	
	length = 2;
	send_buf[0] = 0x02;
	send_buf[1] = 0x09;
	send_buf[2] = length;
	send_buf[3] = WIFI_TEXT_SET;
	send_buf[4] = 0x10;
	send_buf[5] = 0x03;
	send_buf[6] = 0x03;
	send_buf[7] = checksum(send_buf+1,send_buf[2]+4);
}

uint8_t my_wifi_cmd = 0;
uint8_t wifi_comm_process(uint8_t cmd)
{
	uint8_t unlink_send_wait_time=0;//aaron，增加这个量用来存储发送解网命令的间隔时间
	wifi_commtimes++;
	switch(cmd)
	{
		my_wifi_cmd = cmd;
		case WIFI_INTI:
			//以前配过网络，断电上电后，自动配网
			if(wifi_get_buff[4]==0x10&&wifi_get_buff[5]==0x02)
			{
				if(wifi_get_buff[6]==0x31){
					wifi_stat.smart_link_stat=SMARTLINK_SUCCESS;
					cmd=WIFI_ACK_2;	
				}	
				if(wifi_get_buff[6]==0x30){
					cmd=NET_STAT;
					wifi_stat.smart_link_stat=SMARTLINK_FAILED;	
				}
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
				smartlink=1; //Aaron，smartlink有没有完成标志位
				smartlink_start=1;
				success_wifi=0;//aaron，是否链接成功路由和服务器
				Get_Time(TIME_CLEAN,&wifitime);
				wifi_commtimes=0;
			}
		break;
		case WIFI_START :

		    //aaron ,收到10 09 31，停止发送10 08 31开始配网信号
			if (wifi_get_buff[4]==0x10&&wifi_get_buff[5]==0x09&&wifi_get_buff[6]==0x31)
			{
				agree_send_wifi_start_msg = 0;
			}
			
			//人工配网。发送配网信号
			//memset(send_buf,0,sizeof(send_buf));
			if(wifi_UART_send)
			{
				if (agree_send_wifi_start_msg)
				{
					wifi_start_massage();
					wifi_UART_send=0;
					R_UART1_Send(send_buf,send_buf[2]+6);
					wifi_commtimes=0;
					cmd=WIFI_START_ACK;
					last_check_num=0;
				}
			}
			
		break;
		//Aaron，发送解网数据
		case WIFI_UNLINK:

			if(wifi_get_buff[5]==0x0b)
			{
				get_unlink_feedback=1;
			}

			Get_Time(UnitSec,&wifi_unlink_time);
			if (wifi_unlink_time.time_sec <= wifi_unlink_wait_period)
			{
				wifi_UART_send=0;
				//memset(send_buf,0,sizeof(send_buf));
				wifi_unlink_message();
				R_UART1_Send(send_buf,send_buf[2]+6);
				
				if(get_unlink_feedback==0)
				{
					cmd = WIFI_UNLINK; 
				}
				else if(get_unlink_feedback==1)
				{
					//如果解网成功，清除wifi_stat
					display_wifi(0);
					cmd=WIFI_INTI; //如果解网成功，返回到初始化状态
					smartlink = 0;
					dis_smartlink = 0;
					smartlink_start = 0;
					get_unlink_feedback=0;
				}
				
				Get_Time(TIME_CLEAN,&wifi_unlink_time);
			}
			
			if (get_unlink_feedback==1)
			{
				//如果回复了0x0b，表示解网成功，清除wifi_stat
				
				wifi_stat.net_stat = OFFLINE;
				wifi_stat.smart_link_stat = SMARTLINK_FAILED;
				smartlink_start = 0;
				success_wifi = 0;
				//memset(wifi_get_buff,0,sizeof(wifi_get_buff));
				unlink_send_wait_time=0;
				cmd=WIFI_INTI;//返回到初始化状态
			}
			//else if (unlink_send_wait_time>1000)//如果超过1秒没有回复，表示解网失败
			//{
			//	unlink_send_wait_time=0;
			//	cmd=WIFI_INTI;//返回到初始化状态
			//}
			//else
			//{
				/* code */
			//}
		break;
		case WIFI_START_ACK:
			//start具备重发，重发时间为。在没收到09 31 每秒五次
			if(wifi_get_buff[3]==0x41)
			{
				cmd=SIGNAL_LIMIT;//这里注销，表示一直进行配网发送，不接受09 31
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
				//wifi_commtimes=0;
			}
		break;
		case SIGNAL_LIMIT://接受配网代码    7
			if(wifi_get_buff[4]==0x10&&wifi_get_buff[5]==0x09)
			{
				cmd++;
				if(wifi_get_buff[6]==0x31){
					smartlink=1;
				}
				else if(wifi_get_buff[6]==0x30)
				{
					smartlink=0;
				}
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
				wifi_commtimes=0;
			}
			//if(wifi_commtimes>6){
			//	return WIFI_START;
			//}
			break;
		case WIFI_ACK_1://回复ack    8
			if(wifi_UART_send&&wifi_commtimes>=2)
			{
				wifi_UART_send=0;
				//memset(send_buf,0,sizeof(send_buf));
				wifi_ack_massage();
				R_UART1_Send(send_buf,send_buf[2]+6);
				//判断上一步是否为0x31，如果是，下一步，不是则退回去继续接受
				if(smartlink==1)
				{
					cmd++;
				}
				else if(smartlink==0)
				{
					cmd=SIGNAL_LIMIT;
				}
				wifi_commtimes=0;
			}
			break;
		case NET_STAT:   //5
			if(wifi_get_buff[4]==0x10&&wifi_get_buff[5]==0x02)
			{
				cmd++;
				if(wifi_get_buff[6]==0x31){
					wifi_stat.smart_link_stat=SMARTLINK_SUCCESS;
				}	
				else if(wifi_get_buff[6]==0x30){
					wifi_stat.smart_link_stat=SMARTLINK_FAILED;
				}
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
				wifi_commtimes=0;
			}
			//if(dis_comm&&wifi_commtimes>=2)
			//{
			//	wifi_NAK_massage();
			//	R_UART1_Send(send_buf,send_buf[2]+6);
			//	dis_comm=0;
			//	wifi_commtimes=0;
			//}
			//wifi_commtimes=0;
			break;
		case WIFI_ACK_2://回复ack 6
			if(wifi_UART_send&&wifi_commtimes>=2)
			{
				wifi_UART_send=0;
				wifi_ack_massage();
				R_UART1_Send(send_buf,send_buf[2]+6);
				if(wifi_stat.smart_link_stat==SMARTLINK_SUCCESS)
				{
					cmd++;
				}
				else if(wifi_stat.smart_link_stat==SMARTLINK_FAILED)
				{
					cmd=NET_STAT;
				}
				wifi_commtimes=0;
			}
			//dishhhhhh=0;
			break;
		case LINK_STAT:  //7    
			//dishhhhhh++;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
			if(wifi_get_buff[4]==0x10&&wifi_get_buff[5]==0x05)
			{
				cmd++;
				if(wifi_get_buff[6]==0x31){
					wifi_stat.net_stat=ONLINE;
					success_wifi=1;
				}	
				else if(wifi_get_buff[6]==0x30){
					wifi_stat.net_stat=OFFLINE;
				}
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
				wifi_commtimes=0;
			}
			break;
		case WIFI_ACK_3:
			if(wifi_UART_send&&wifi_commtimes>=2)
			{
				wifi_UART_send=0;
				wifi_ack_massage();
				R_UART1_Send(send_buf,send_buf[2]+6);
				if(wifi_stat.net_stat==ONLINE)
				{
					cmd=WIFI_F0_F1_F2_GET;
				}
				else if(wifi_stat.net_stat==OFFLINE)
				{
					cmd=LINK_STAT;
				}
			}
			
			break;
		case WIFI_F0_F1_F2_GET:
			if(wifi_get_buff[3]==0x47)
			{
				switch (wifi_get_buff[4])
				{
				case 0xf0:
					cmd=WIFI_F0_RES;
					break;
				case 0xf1:
					cmd=WIFI_F1_RES;
					break;
				case 0xf2:
					cmd=WIFI_F2_RES;
					break;
				default:
					break;
				}
				wifi_commtimes=0;
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
			}
		break;
		case WIFI_F0_RES:
			if(wifi_UART_send&&wifi_commtimes>=2)
			{
				wifi_UART_send=0;
				memcpy(send_buf,get_f0_buff,get_f0_buff[2]+6);
				R_UART1_Send(send_buf,send_buf[2]+6);
				cmd=WIFI_F0_F1_F2_GET;
				wifi_commtimes=0;
			}
		break;
		case WIFI_F1_RES:
			if(wifi_UART_send&&wifi_commtimes>=2)
			{
				wifi_UART_send=0;
				wifi_f1_massage();
				R_UART1_Send(send_buf,send_buf[2]+6);
				memcpy(check_buf,send_buf,send_buf[2]+6);
				//check_buf[3]=0x49;
				last_check_num=1;
				cmd=WIFI_F0_F1_F2_GET;
				wifi_commtimes=0;
			}
		break;
		case WIFI_F2_RES:
			if(wifi_UART_send&&wifi_commtimes>=2)
			{
				wifi_UART_send=0;
				memcpy(send_buf,get_f2_buff,get_f2_buff[2]+6);

				R_UART1_Send(send_buf,send_buf[2]+6);
				//memset(get_f2_buff,0,sizeof(get_f2_buff));
				send_buf[3]=TEXT_RES;
				send_buf[send_buf[2]+5] = checksum(send_buf+1,send_buf[2]+4);
				cmd=WIFI_RUN;
				dis_f2_send=1;
				wifi_commtimes=0;
			}
		break;
		case WIFI_INFOR:
			//收到WiFi发来的其他信息，优先处理
			if((wifi_get_buff[4]==0x10||wifi_get_buff[4]==0xf4)&&dis_count==0)
			{
				cmd=WIFI_SEND_ACK_RES_IS_OK;
				break;
			}
			//再次处还要判断是否网络连接
			//进入这一步时候，按键主板已经更改，但是需要判断与上一次通信结束是否大于等于200ms
			if(wifi_commtimes>=21&&wifi_UART_send==1&&dis_signal_stat==0)
			{
				if(wifi_stat.net_stat==ONLINE)
				{
					wifi_UART_send=0;
					//dishhhhhh=wifi_commtimes;
					//P2_bit.no2=0;
					memset(send_buf,0,sizeof(send_buf));
					wifi_infor_massage();
					R_UART1_Send(send_buf,send_buf[2]+6);
					//last_check_num=send_buf[25];
					cmd++;
					wifi_commtimes=0;
				}
				else if(wifi_stat.net_stat==OFFLINE)
				{
					cmd=WIFI_SEND_ACK_RES_IS_OK;
				}
			}
			break;
		case WIFI_RES_1:
			//wifi_commtimes++;
			//收到WiFi发来的其他信息，优先处理
			if(wifi_get_buff[3]==0x41)
			{
				cmd=WIFI_SEND_ACK_RES_IS_OK;
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
				wifi_commtimes=0;
			}
			else if(wifi_get_buff[3]==0x4E)
			{
				wifi_commtimes=11;
				//dis_count=0;
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
			}
			else if(wifi_get_buff[3]==0x47)
			{
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
			}
			if(wifi_commtimes>10)
			{
				cmd=WIFI_INFOR;
				wifi_commtimes=19;
				dis_count++;
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
				if(dis_count==3)
				{
					cmd=WIFI_SEND_ACK_RES_IS_OK;
					//dis_count=0;
				}
			}
			
		break;
		case WIFI_RUN:

			// if (wifi_ctrl_block_time>0)
			// {
			// 	memset(wifi_get_buff,0,sizeof(wifi_get_buff));
			// 	wifi_commtimes=0;
			// 	break;
			// } //修改按键操作3秒内锁定WiFi操作，这里不需要

			//wifi_commtimes=0;
			//防止时间过长，造成的数据变为<20
			if(wifi_commtimes>=21)
			{
				wifi_commtimes=21;
			}
			//以下为WiFi发来的开关命令
			if(wifi_get_buff[4]==0xf4)
			{
				if(wifi_get_buff[5]==0x00)
				{
					key_flag=1;key_num=1;
					//memset(wifi_get_buff,0,sizeof(wifi_get_buff));
					cmd=WIFI_ACK;
				}
				if(wifi_get_buff[5]==0x01)
				{
					key_flag=1;key_num=2;
					//memset(wifi_get_buff,0,sizeof(wifi_get_buff));
					cmd=WIFI_ACK;
				}
				if(wifi_get_buff[5]==0x02)
				{
					heater_info.eco_mode=wifi_get_buff[6];
					key_num=8;
					key_flag=1;
					//memset(wifi_get_buff,0,sizeof(wifi_get_buff));
					cmd=WIFI_ACK;
				}
				if(wifi_get_buff[5]==0x03)
				{
					
					heater_info.Send_SetHeatTemp = wifi_get_buff[6];
					key_flag=1;key_num=3;
					//memset(wifi_get_buff,0,sizeof(wifi_get_buff));
					cmd=WIFI_ACK;
				}
				if(wifi_get_buff[5]==0x04)
				{
					if(heater_info.akey_cycle==0x31&&wifi_get_buff[6]==0x31)
					{
						disakey_cycle=0;
					}
					else 
					{
						key_flag=1;key_num=4;
						heater_info.send_akeycycle=wifi_get_buff[6];
					}
					//增压在开启的时候，循环打开。
					if(heater_info.super_mode==0x31&&wifi_get_buff[6]==0x31)
					{
						super_is_ok=1;
					}
					//memset(wifi_get_buff,0,sizeof(wifi_get_buff));
					cmd=WIFI_ACK;
				}
				if(wifi_get_buff[5]==0x05)
				{
					heater_info.send_cycle_mode=wifi_get_buff[6];
					key_flag=1;key_num=5;
					//memset(wifi_get_buff,0,sizeof(wifi_get_buff));
					cmd=WIFI_ACK;
				}
				if(wifi_get_buff[5]==0x06)
				{
					//操作部在增压灯和循环灯都亮的时候，发来关闭增压的命令。
					if(wifi_get_buff[6]==0x30&&super_is_ok==1)
					{
						super_is_ok=0;
					}
					else if(wifi_get_buff[6]==0x31&&heater_info.akey_cycle==0x31)
					{
						super_is_ok=1;
					}
					else
					{
						heater_info.send_super_mode=wifi_get_buff[6];
						key_flag=1;key_num=6;
					}
					//memset(wifi_get_buff,0,sizeof(wifi_get_buff));
					cmd=WIFI_ACK;
				}
				if(wifi_get_buff[5]==0x07)
				{
					//if(wifi_get_buff[6]!=dis_runmode)//条件成立，表示运行模式改变，
					//{
					//	runing_mode[dis_runmode].init_temp = heater_info.Send_SetHeatTemp;
					//	dis_runmode=wifi_get_buff[6];
					//	heater_info.running_mode=dis_runmode;
					//	heater_info.Send_SetHeatTemp=runing_mode[dis_runmode].init_temp;
					//	key_flag=1;key_num=3;
					//}
					runing_mode[dis_runmode].init_temp = heater_info.Send_SetHeatTemp;
					dis_runmode=wifi_get_buff[6];
					//heater_info.running_mode=dis_runmode;
					heater_info.Send_SetHeatTemp=runing_mode[dis_runmode].init_temp;
					//heater_info.SetHeatTempMax=runing_mode[dis_runmode].max_temp;
					//heater_info.SetHeatTempMin=runing_mode[dis_runmode].min_temp;
					
					key_flag=1;
					key_num=3;
					
					cmd=WIFI_ACK;
				}
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
				wifi_commtimes=0;
			}
			
			//发来数据不对，回复NAK
			if(dis_comm&&wifi_commtimes>=2&&wifi_UART_send)
			{
				wifi_UART_send=0;
				wifi_NAK_massage();
				R_UART1_Send(send_buf,send_buf[2]+6);
				dis_comm=0;
				wifi_commtimes=0;
			}
		break;
		case WIFI_ACK :
			//保证上一次发送完成
			if(wifi_UART_send&&wifi_commtimes>=2)
			{
				//P2_bit.no2=0;
				wifi_UART_send=0;
				wifi_ack_massage();
				R_UART1_Send(send_buf,send_buf[2]+6);
				cmd=WIFI_RUN;
				wifi_commtimes=0;
			}
			
		break;
		case WIFI_REset:
			if(wifi_UART_send&&wifi_commtimes>=20)
			{
				//wifi二十分钟没有发来信号强度，则发送这个
				wifi_UART_send=0;
				//memset(send_buf,0,sizeof(send_buf));
				wifi_reset_data();
				R_UART1_Send(send_buf,send_buf[2]+6);
				cmd=WIFI_ACK_5;
				wifi_commtimes=0;
			}
		break;
		case WIFI_ACK_5:
			//wifi_commtimes++;
			if(wifi_get_buff[3]==0x41)
			{
				cmd=WIFI_RESET_1;
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
				wifi_commtimes=0;
			}
			else if(wifi_get_buff[3]==0x4E)
			{
				wifi_commtimes=11;
				//dis_count=0;
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
			}
			if(wifi_commtimes>10)
			{
				cmd=WIFI_REset;
				wifi_commtimes=0;
				dis_count++;
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
				if(dis_count>=3)
				{
					cmd=WIFI_RESET_1;
				}
			}
			
		break;

		case WIFI_RESET_1:
			if(wifi_get_buff[5]==0x04)
			{
				cmd++;
				wifi_commtimes=0;
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
			}
		break;
		case WIFI_RESET_ACK:
			if(wifi_UART_send&&wifi_commtimes>=2)
			{
				wifi_UART_send=0;
				wifi_ack_massage();
				R_UART1_Send(send_buf,send_buf[2]+6);
				cmd=WIFI_INTI;
				wifi_commtimes=0;
				smartlink=0;
				smartlink_start=0;
				wifi_stat.smart_link_stat=SMARTLINK_FAILED;
				wifi_stat.net_stat=OFFLINE;
			}
		break;
		
		case WIFI_Information :
			//如果在发送的时候，有其他数据发来，那么就不发了。
			if(wifi_get_buff[4]!=0x00)
			{
				cmd=WIFI_SEND_ACK_RES_IS_OK;
				//memcpy(get_f2_buff,send_buf,send_buf[2]+6);
				break;
			}
			if(wifi_UART_send&&dis_signal_stat==0)
			{
				wifi_UART_send=0;
				memcpy(send_buf,get_f2_buff,get_f2_buff[2]+6);
				//memset(get_f2_buff,0,sizeof(get_f2_buff));
				//用于发送机械信息和故障代码
				send_buf[3]=TEXT_INF;
				//for(;iz<24;iz++)
				//{
				//	send_buf[iz]=send_buf[iz]+numberaa;
				//}
				//iz=6;
				send_buf[send_buf[2]+5] = checksum(send_buf+1,send_buf[2]+4);
				R_UART1_Send(send_buf,send_buf[2]+6);
				cmd++;
				dis_f2_send=1;
				wifi_commtimes=0;
			}
			
		break;
		case WIFI_RES_2:
			
			if(wifi_get_buff[3]==0x41)
			{
				cmd=WIFI_SEND_ACK_RES_IS_OK;
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
				//memset(get_f2_buff,0,sizeof(get_f2_buff));
				wifi_commtimes=0;
			}
			else if(wifi_get_buff[3]==0x4E)
			{
				wifi_commtimes=11;
				//dis_count=0;
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
			}
			else if(wifi_get_buff[3]==0x47)
			{
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
			}
			if(wifi_commtimes>10)
			{
				cmd=WIFI_Information;
				wifi_commtimes=0;
				dis_count++;
				memset(wifi_get_buff,0,sizeof(wifi_get_buff));
				if(dis_count>=3)
				{
					cmd=WIFI_SEND_ACK_RES_IS_OK;
					//memset(get_f2_buff,0,sizeof(get_f2_buff));
					//break;
				}
			}
		break;
		case WIFI_SEND_ACK_RES_IS_OK:
			dis_count=0;
		break;
//		case SIGNAL_LIMIT ://get	
//		break;
//		case NET_STAT ://get		
//		break;
//		case LINK_STAT ://get
		
//		break;
		
	}
	//接收到WiFi信号强度的
	if(wifi_get_buff[4]==0x10&&wifi_get_buff[5]==0x01&&dis_signal_stat==0)
	{
		wifi_stat.signal = LOW;		
		dis_signal_stat=1;
		Get_Time(TIME_CLEAN,&wifi_signal);
		memset(wifi_get_buff,0,sizeof(wifi_get_buff));
	}
	//以下为在运行中和服务器是否断开
	else if(wifi_get_buff[5]==0x02&&wifi_get_buff[4]==0x10&&dis_signal_stat==0)
	{
		if(wifi_get_buff[6]==0x30)
		{
			//dis_signal--;
			wifi_stat.smart_link_stat=SMARTLINK_FAILED;
			wifi_stat.net_stat=OFFLINE;
			//last_check_num=0;
			memset(wifi_get_buff,0,sizeof(wifi_get_buff));
			//网络断开了，此时把它设置为初始值，后面再次连接上后，也上报一次
		}
		else if(wifi_get_buff[6]==0x31)
		{
			//dis_signal++;
			//last_check_num=0;
			wifi_stat.smart_link_stat=SMARTLINK_SUCCESS;
			memset(wifi_get_buff,0,sizeof(wifi_get_buff));
		}
		dis_signal_stat=1;
	}
	else if(wifi_get_buff[5]==0x05&&wifi_get_buff[4]==0x10&&dis_signal_stat==0)
	{
		if(wifi_get_buff[6]==0x30)
		{
			//dis_signal--;
			wifi_stat.net_stat=OFFLINE;
			
			//网络断开了，此时把它设置为初始值，后面再次连接上后，也上报一次
			//last_check_num=0;
		}
		else if(wifi_get_buff[6]==0x31)
		{
			//dis_signal++;
			success_wifi=1;
			wifi_stat.net_stat=ONLINE;
			wifi_stat.smart_link_stat=SMARTLINK_SUCCESS;
			//if(cmd<WIFI_INFOR)
			//{
				cmd=WIFI_F0_F1_F2_GET;
			//}
			//last_check_num=0;
		}
		memset(wifi_get_buff,0,sizeof(wifi_get_buff));
		dis_signal_stat=1;
	}
	else if(dis_signal_stat!=0)
	{
		dis_signal_stat++;
		if(dis_signal_stat>=3&&wifi_UART_send)
		{
			wifi_UART_send=0;
			wifi_ack_massage();
			R_UART1_Send(send_buf,send_buf[2]+6);
			dis_signal_stat=0;
		}
		wifi_commtimes=0;
	}
	return cmd;
}
// TEXT_SET        0x53
uint8_t wifi_ee=0;
uint8_t wifi_i,wifi_buf[60]={0};
uint8_t wifi_numlength=0,wifi_numlength1;
void  wifi_get_process(uint8_t wifi_data)
{
	static uint8_t comData=0;
	//Aaron,修改按键操作3秒内锁定WiFi操作，按键操作锁定时间3秒内，不接收WiFi数据
	if (wifi_ctrl_block_time>0)
	{
		memset(wifi_buf,0,sizeof(wifi_buf));
		memset(wifi_get_buff,0,sizeof(wifi_get_buff));
		return;
	} 
	
    //if( sendover_flag){sendover_flag=0;i=0;}
	switch (comData)
	{
	
	case WIFI_STXdata:
		if(wifi_data==stx_date)
		{
			wifi_buf[wifi_i]=wifi_data;
			wifi_i++;
			comData++;
		}
		break;
	case WIFI_SORTdata:
		if(wifi_data==sort_date)
		{
			wifi_buf[wifi_i]=wifi_data;
			wifi_i++;
			comData++;
		}
		else
		{
			wifi_i=0;
			comData=WIFI_STXdata;
			dis_comm=1;
		}
		break;
	case WIFI_LENGTHdata:
		if((wifi_data+6)<=60)
		{
			wifi_buf[wifi_i]=wifi_data;
			wifi_i++;
			comData++;
			wifi_numlength=wifi_data;
		}
		else
		{
			wifi_i=0;
			comData=WIFI_STXdata;
			dis_comm=1;
		}
		break;
	case WIFI_COMMANDdata:
		if(wifi_data==WIFI_TEXT_SET||wifi_data==WIFI_TEXT_GET||wifi_data==WIFI_TEXT_RES||wifi_data==WIFI_TEXT_INF||wifi_data==WIFI_TEXT_ACK||wifi_data==WIFI_TEXT_NAK)
		{
			wifi_buf[wifi_i]=wifi_data;
			wifi_i++;
			comData++;
		}
		else
		{
			wifi_i=0;
			comData=WIFI_STXdata;
			dis_comm=1;
		}


		
		if(wifi_numlength==0)
		{
			comData++;
		}
		break;
	case WIFI_TEXTdata:
		
		if(wifi_numlength==1)
		{
			comData++;
		}
		wifi_buf[wifi_i]=wifi_data;
		wifi_i++;
		wifi_numlength--;
		break;
	case WIFI_ETXdata:
		//wifi_re_date=wifi_data;	
		if(wifi_data==etx_date)
		{
			wifi_buf[wifi_i]=wifi_data;
			wifi_i++;
			comData++;
		}
		else
		{
			wifi_i=0;
			comData=WIFI_STXdata;
			dis_comm=1;
		}
		break;
	case WIFI_BCCdata:
		wifi_numlength1 = checksum(wifi_buf+1,wifi_buf[2]+4);
		//wifi_re_date=wifi_data;
		// if(wifi_buf[5]==0x0b){
		// 	get_unlink_feedback = 1;
		// 	memcpy(wifi_get_buff,wifi_buf,wifi_buf[2]+6);
		// 	memset(wifi_buf,0,sizeof(wifi_buf));
		// }
		if(wifi_numlength1==wifi_data)
		{
			wifi_buf[wifi_i]=wifi_data;
			//if(wifi_buf_buff[0]==0&&wifi_get_buff[0]==0)
			//{
			memcpy(wifi_get_buff,wifi_buf,wifi_buf[2]+6);
			memset(wifi_buf,0,sizeof(wifi_buf));
			//dishhhhhh++;
			//}
			//else if(wifi_get_buff[0]==0x02&&wifi_buf_buff[0]==0)
			//{
			//	memcpy(wifi_buf_buff,wifi_buf,wifi_buf[2]+6);
			//	memset(wifi_buf,0,sizeof(wifi_buf));
			//}
			//commfail_cnt=0;
			//Commfail_Flag=0;
			//wifi_get_data();
		}
		else
		{
			dis_comm=1;
		}
		wifi_i=0;
		comData=WIFI_STXdata;
	}
	//if(wifi_buf_buff[0]==0x02&&wifi_get_buff[0]==0)
	//{
	//	memcpy(wifi_get_buff,wifi_buf_buff,wifi_buf_buff[2]+6);
	//	memset(wifi_buf_buff,0,sizeof(wifi_buf_buff));
	//}
}
/*uint8_t wifi_get_process(uint8_t date)
{
	static uint8_t i,buf[60]={0};
    
	buf[i]=date;
	i++;
	if(buf[0]==0x2 && buf[1]==0x9 && i>=(buf[2]+6))
	{
        i=0;
		memcpy(wifi_get_buff,buf,buf[2]+6);
		memset(buf,0,sizeof(buf));
//		commfail_cnt=0;
//		Commfail_Flag=0;
		wifi_get_data();
	}
       
 	return 0;
}




uint8_t uart_buf[100];
void wifi_get_process(uint8_t dat)
{
	static uint8_t  UART_step=0,i=0;
	static uint8_t ubtRecFrameLength = 0;
	//static uint8_t uart_buf[100];
//	static uint8_t checksum_val=0;
//	i++;
//	if(i<20){
//		uart_buf[i]=dat;
//		i=0;
//	}
	switch(UART_step)
	{
    	case 0:
            UART_step=0;
	 		uart_buf[0]=dat;
            if( dat==0x02){
                UART_step=1;
	    		uart_buf[0]=dat;
	    	}
            break;
        case 1:
			UART_step=0;
            if( dat==0x09){
				UART_step=1;
				uart_buf[1]=dat;
			}
//			else {
//                UART_step=2;
//				ubtRecFrameLength=dat;
//				uart_buf[1]=ubtRecFrameLength;
//        		i=0;
//			}
            break;
        case 2:
            uart_buf[i+2]=dat;
            i++;
            if(i>=(ubtRecFrameLength)){
                UART_step=0;
	    		memcpy(get_data.buf,uart_buf,ubtRecFrameLength+2);
//				checksum_val=checksum(&uart_buf[4],ubtRecFrameLength-3);
//				if(checksum_val=uart_buf[i]){
//					get_data_process();
//				}
	    		get_data_process();
            }
            break;
        default:
        	i=0;
            break;
	}
}
*/